package mybox.engine;

import java.util.ArrayList;

public class Arc implements Figur {
	
	public File4Qcad file4qcad=null;
	public QVector center=null;
	public double radius;
	public QVector begin;
	public QVector end;
	public int aufloesung;
	
	//anticlockwise
	ArrayList<QVector> polygon = new ArrayList<QVector>();
	
	public Arc(File4Qcad file4qcad, QVector center, QVector begin, QVector end, int aufloesung) {
		this.file4qcad=file4qcad;
		this.center=center;
		this.begin=begin;
		this.end=end;
		this.radius=QVector.diff(center,begin).length();
		this.aufloesung=aufloesung;
		initPolygon(center,begin,end,aufloesung);
	}
	
	private void initPolygon(QVector center, QVector begin, QVector end, double deltaAlpah) {
		double alphaDiff=antiClockWiseDiff(center, begin, end);
		double deltaAlpha=(alphaDiff)/aufloesung;		
		QVector currentPoint=begin;		
		polygon.add(begin);		
		for (int alphaIdx=0;alphaIdx<aufloesung;alphaIdx++) {
			double currentAlpha=Math.atan2(currentPoint.y-center.y, currentPoint.x-center.x);
			double nextAlpha=currentAlpha+deltaAlpha;			
			QVector nextPoint = new QVector (
						center.x+(radius*Math.cos(nextAlpha)),
						center.y+(radius*Math.sin(nextAlpha))
					);
			polygon.add(nextPoint);
			currentPoint=nextPoint;
		}
		
	}

	@Override
	public void draw() {
		for (int polIdx=0;polIdx<polygon.size()-1;polIdx++) {
			file4qcad.println(QVector.printLine(polygon.get(polIdx), polygon.get(polIdx+1)));
		}	
	}
	
	public static double antiClockWiseDiff(QVector center, QVector begin, QVector end) {
		double beginAlpha=Math.atan2(begin.y-center.y, begin.x-center.x);
		double endAlpha=Math.atan2(end.y-center.y, end.x-center.x);
		double alphaDiff=endAlpha-beginAlpha;
		if (alphaDiff<0) alphaDiff+=2*Math.PI;
		return alphaDiff;
	}

}
